package pogvue.gui.popups;

import pogvue.gui.AlignViewport;
import pogvue.gui.Controller;
import pogvue.gui.event.AlignViewportEvent;

import javax.swing.*;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import java.awt.*;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

public final class PIDBaselinePopup extends Popup implements ChangeListener, MouseListener {
  private final JLabel tfLabel;
  private final JSlider slider;
   private boolean mouseDown = false;

  public PIDBaselinePopup(JPanel parent,AlignViewport av,Controller c,String title) {
    super(parent,av,c,title);
    tfLabel = new JLabel("Baseline identity : ");
    slider  = new JSlider(JSlider.HORIZONTAL,0,100,av.getPIDBaseline());
    slider.setMajorTickSpacing(30);
    slider.setMinorTickSpacing(10);
    slider.setPaintTicks(true);
    slider.setPaintLabels(true);
    slider.setValue(av.getPIDBaseline());
    slider.addChangeListener(this);
    slider.addMouseListener(this);


    gbc.fill = GridBagConstraints.NONE;

    gbc.insets = new Insets(20,20,20,20);


    add(apply,gb,gbc,0,3,1,1);
    add(close,gb,gbc,1,3,1,1);

  }

    public void mouseClicked(MouseEvent e) {
    }
    public void mousePressed(MouseEvent e) {
	mouseDown = true;
	System.out.println("Mouse is pressed");

    }
    public void mouseReleased(MouseEvent e){
	System.out.println("Mouse is released");
	mouseDown = false;
	stateChanged(new ChangeEvent(this));
    }
    public void mouseEntered(MouseEvent e){}
    public void mouseExited(MouseEvent e) {}


    public void stateChanged(ChangeEvent e) {
	int value = slider.getValue();
	System.out.println("Mousedown " + mouseDown);
     
     if (value != av.getPIDBaseline() && !mouseDown) {
       av.setPIDBaseline(value);
       controller.handleAlignViewportEvent(new AlignViewportEvent(this,av,AlignViewportEvent.SHOW));
     }
  } 
}
